package com.example.rbacmanage.controller.background;

import com.example.rbacmanage.common.Result;
import com.example.rbacmanage.entity.app.Activity;
import com.example.rbacmanage.exception.MyException;
import com.example.rbacmanage.service.app.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/background/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @GetMapping("/page")
    public Result page(Activity activity){
        return Result.success(activityService.getPage(activity));
    }

    @GetMapping("/{id}")
    public Result info(@PathVariable Long id){
        return Result.success(activityService.getById(id));
    }

    @PreAuthorize("hasAnyAuthority('background:activity:add')")
    @PostMapping("/add")
    public Result addActivity(@RequestBody Activity activity) throws MyException {
        activityService.saveActivity(activity);
        return Result.success();
    }


    @PreAuthorize("hasAnyAuthority('background:activity:update')")
    @PutMapping("/update")
    public Result updateActivity(@RequestBody Activity activity) throws MyException {
        activityService.updateActivity(activity);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('background:activity:remove')")
    @DeleteMapping("/remove")
    public Result remove(@RequestBody Long[] ids){
        activityService.remove(ids);
        return Result.success();
    }
}
